<?php
/**
 * 数据库实现类
 */

defined('ACC')||exit('ACC Denied');
class Mysql extends Db {
    private static $ins = NULL;
    private $conn = NULL;
    private $conf = array();

    protected function __construct() {
        $this->conf = Conf::getIns();
        $this->connect($this->conf->host,$this->conf->user,$this->conf->pwd);
    }

    public function __destruct() {}

    public static function getIns() {
        if(!(self::$ins instanceof self)) {
            self::$ins = new self();
        }
        return self::$ins;
    }

    public function connect($h,$u,$p) {
        $this->conn = mysqli_connect($this->conf->host,$this->conf->user,$this->conf->pwd,$this->conf->db); 
        //设置编码
    	mysqli_set_charset($this->conn,"utf8");
        //连接错误，抛出异常
        if (!$this->conn) {
            $err = new Exception('连接失败');
            throw $err;
        }
    }

    public function query($sql) {
		$rs = mysqli_query($this->conn,$sql);
        log::write($sql);
        return $rs;
    }

    public function autoExecute($table,$arr,$mode='insert',$where = ' where 1 limit 1') {
        if(!is_array($arr)) {
            return false;
        }
        if($mode == 'update') {
            $sql = 'update ' . $table .' set ';
            foreach($arr as $k=>$v) {
                $sql .= $k . "='" . $v ."',";
            }
            $sql = rtrim($sql,',');
            $sql .= $where;
            return $this->query($sql);
        }
        $sql = 'insert into ' . $table . ' (' . implode(',',array_keys($arr)) . ')';
        $sql .= ' values (\'';
        $sql .= implode("','",array_values($arr));
        $sql .= '\')';
        return $this->query($sql);
    
    }

    public function getAll($sql) {
    	$rs = $this->query($sql);
        $list = array();
        while($row = mysqli_fetch_assoc($rs)) {
            $list[] = $row;
        }
        return $list;
    }

    public function getRow($sql) {
        $rs = $this->query($sql);
        return mysqli_fetch_assoc($rs);
    }

    public function getOne($sql) {
        $rs = $this->query($sql);
        $row = mysqli_fetch_row($rs);
        return $row[0];
    }

    // 返回影响行数的函数
    public function affected_rows() {
        return mysqli_affected_rows($this->conn);
    }

    // 返回最新的auto_increment列的自增长的值
    public function insert_id() {
        return mysqli_insert_id($this->conn);
    }


}
